<template>
    <div class="address">
        <!--<van-nav-bar title="新增收货地址" left-arrow @click-left="goBack" />-->
        <!-- 地址列表内容 -->
        <!-- 编辑主体 -->
        <div class="content">
            <van-address-edit :area-list="areaList" :address-info="addressInfo" show-postal
                :show-delete="isShowDeleteBtn" show-set-default @save="onSave" @delete="onDelete" />
        </div>
    </div>
</template>

<script>
    import Vue from 'vue'
    import { NavBar, Toast, AddressEdit } from 'vant';
    import Cookies from 'js-cookie';
    import { isDev } from "@/utils/tools";
    import areaList from '@/assets/json/city.js'

    Vue.use(AddressEdit).use(NavBar);

    export default {
        name: "recommendAddressEdit",
        data() {
            return {
                queryIdx: "",
                id:"",
                areaList,
                addressInfo: {},
                isShowDeleteBtn: false,
                activityId:'',
                scheduleId:''
            }
        },
        created() {
            //判断新增或修改
            if (this.$route.query.idx >= 0) {
                this.id = this.$route.query.id;
                this.getInfo();
            }

            if (null!=this.$route.query.activityId) {
                this.activityId = this.$route.query.activityId;
            }

            if (null!=this.$route.query.scheduleId) {
                this.scheduleId = this.$route.query.scheduleId;
            }
        },
        methods: {
            // 回退
            goBack() {
                this.$router.go(-1);
            },
            toList(){
                //判断是否返回中奖达标记录页
                if(null!=this.scheduleId&&''!=this.scheduleId){
                    this.toScheduleDetail();
                    return;
                }
                this.$router.push({
                    path: "/recommend_address"
                });
            },
            toScheduleDetail(){
                this.$router.push({
                    path: "/recommend_scheduleDetail",
                    query: {
                        id: this.scheduleId
                    }
                });
            },
            onSave(e) {

                this.token = Cookies.get("token")
                let data = {};
                data.openId = this.token;
                data.name = e.name;
                data.tel = e.tel;
                data.address = e.province + e.city + e.county + e.addressDetail + "";
                data.addressDetail = e.addressDetail;
                data.areaCode = e.areaCode;
                data.postalCode = e.postalCode;
                data.isDefault = e.isDefault?1:0;
                if (this.id !== "") {
                    data.id = this.id;
                    this.update(data);
                } else {
                    this.save(data);
                }
            },
            getInfo(){
                 this.$api.personInf.userAddressInfo({"id":this.id}).then(res=>{
                    if(res.code==0){
                        this.addressInfo = res.info;
                        this.addressInfo.isDefault = res.info.isDefault?true:false;
                        this.isShowDeleteBtn = true;
                    }else{
                        Toast(res.msg)
                    }
                }).catch(err=>{
                    console.log(err)
                })


            },
            onDelete(e) {
                this.$api.personInf.userAddresDelete({"id":this.id}).then(res=>{
                    if(res.code==0){
                        this.toList();
                    }else{
                        Toast(res.msg)
                    }
                }).catch(err=>{
                    console.log(err)
                })
            },
            save(data){

                this.$api.personInf.userAddressSave(data).then(res=>{
                    if(res.code==0){
                        //判断是否选择默认
                        if(data.isDefault==1){
                            this.default(res.id);
                        }else{
                            this.toList();
                        }
                    }else{
                        Toast(res.msg)
                    }
                }).catch(err=>{
                    console.log(err)
                })
            },
            update(data){

               this.$api.personInf.userAddressUpdate(data).then(res=>{
                   if(res.code==0){
                       //判断是否选择默认
                       if(data.isDefault==1){
                            this.default(data.id);
                        }else{
                            this.toList();
                        }
                   }else{
                       Toast(res.msg)
                   }
               }).catch(err=>{
                   console.log(err)
               })
           },
           default(userAddressId){
                this.token = Cookies.get("token")
                this.$api.personInf.userAddressSelect({openId:this.token,activityId:this.activityId,userAddressId:userAddressId}).then(res=>{
                    this.toList();
                }).catch(err=>{
                    console.log(err)
                })
           }
        }
    }
</script>

<style lang="less" scoped>
</style>
